<!doctype html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security" xmlns:auth=""
      class="x-admin-sm">
<auth:data uri="/system/res/list.json,/system/res/visible"/>
<head>
    <th:block th:include="common/head::noXadmin('资源管理')"/>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">

        <div class="layui-col-md12">
            <div class="layui-card" id="queryBox">
                <div class="layui-card-body ">
                    <div class="layui-inline layui-show-xs-block">
                        <input class="layui-input " id="edtSearch" placeholder="输入关键字"/>
                    </div>
                    <div class="layui-inline layui-show-xs-block">
                        <button id="btnSearch" class="layui-btn layui-btn-sm ">
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button id="btnClearSearch" class="layui-btn  layui-btn-sm">
                            <i class="layui-icon">&#x1006;</i>清除
                        </button>
                    </div>
                </div>
            </div>
            <div class="layui-card">
                <div class="split-line-y split-line-y-open">
                    <i class="fa "></i>
                </div>
                <div class="layui-card-body">
                    <div style="border: solid 1px #e6e6e6;background-color: #f2f2f2;padding: 5px 5px 0px 5px;border-bottom: none;">
                        <div class="layui-btn-container">
                            <button class="layui-btn layui-btn-sm" onclick="add()">
                                <i class="layui-icon layui-icon-add-circle "></i>添加
                            </button>
                            <button class="layui-btn layui-btn-warm layui-btn-sm" id="btnExpandOrFold">
                                <i class="fa fa-exchange"></i>折叠/展开
                            </button>
                        </div>
                    </div>
                    <div class="layui-table-box">
                        <table id="dataList" lay-filter="data" lay-skin="line"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<th:block th:inline="none">
    <script type="text/html" id="visibleTpl">
        <!-- 这里的checked的状态只是演示 -->
        <input type="checkbox" name="visible" value="{{d.resId}}" lay-skin="switch" lay-text="显示|隐藏"
               {{d.visible==0?"checked":""}} lay-filter="visible">
    </script>
    <script type="text/html" id="barTpl">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>
    <script type="text/html" id="typeTpl">
        {{#  if(d.resType === 'D'){ }}
        <button type="button" class="layui-btn layui-btn-xs">目录</button>
        {{#  } }}
        {{#  if(d.resType === 'M'){ }}
        <button type="button" class="layui-btn layui-btn-xs layui-btn-normal">菜单</button>
        {{#  } }}
        {{#  if(d.resType === 'B'){ }}
        <button type="button" class="layui-btn layui-btn-xs layui-btn-warm">按钮</button>
        {{#  } }}
    </script>
    <script type="text/html" id="nameTpl">
        <li class="{{d.icon}}"> {{d.resName}}</li>
    </script>
    <script>
        layui.use(['jquery'], function () {
            var $ = layui.jquery;
            var queryBoxShow = true;
            $(".split-line-y >i").click(function () {
                if (queryBoxShow) {
                    queryBoxShow = false;
                    $("#queryBox").slideUp('fast');
                    $(".split-line-y").removeClass('split-line-y-open').addClass('split-line-y-closed');
                } else {
                    queryBoxShow = true;
                    $("#queryBox").slideDown('fast');
                    $(".split-line-y").removeClass('split-line-y-closed').addClass('split-line-y-open');
                }

            });
        })

        function add() {
            xadmin.open('添加资源', rootPath + 'system/res/add');
        }

        layui.use(['table', 'form'],
            function () {
                var table = layui.table,
                    form = layui.form;
                form.on('switch(visible)', function (data) {
                    $.ajax({
                        url: rootPath + 'system/res/visible?resId=' + data.value,
                        type: "POST",
                        dataType: "json",
                        success: function (data) {
                            layer.msg(data.message);
                        },
                        error: function (data) {
                            layer.msg(data.message);
                        }
                    });
                })

            });
    </script>
    <script>
        layui.extend({
            treeTable: 'treeTable/treeTable'
        }).use(['layer', 'util', 'treeTable'], function () {
            var $ = layui.jquery;
            var layer = layui.layer;
            var util = layui.util;
            var treeTable = layui.treeTable;
            // 渲染表格
            var insTb = treeTable.render({
                elem: '#dataList',
                skin: 'line',
                pidName: 'parentId',
                tree: {
                    iconIndex: 1,
                    allowType: 'arrow1',
                    getIcon: function (d) {
                        // d是当前行的数据
                        if (d.haveChild) {  // 判断是否有子集
                            return '<i class="ew-tree-icon layui-icon layui-icon-right"></i>';
                        } else {
                            return '<i class="ew-tree-icon  "></i>'
                        }
                    }
                },
                cols: [
                    {checkbox: true, type: 'checkbox', fixed: 'left'}
                    , {field: 'resName', title: '资源名称', templet: '#nameTpl', width: 220}
                    , {field: 'order', title: '排序', width: 80}
                    , {field: 'resPath', title: '请求地址', width: 300}
                    , {field: 'resType', title: '类型', templet: '#typeTpl'}
                    , {field: 'visible', title: '可见', templet: '#visibleTpl'}
                    , {field: 'resMark', title: '标识', width: 200, sigleLine: true,}
                    , {field: 'ctime', title: '创建时间', width: 180}
                    , {fixed: 'right', title: '操作', width: 178, align: 'center', toolbar: '#barTpl'}
                ],
                style: 'margin-top:0;',
                reqData: function (data, callback) {
                    $.get('/system/res/list.json', {}, function (res, status) {

                        var d = res.data.list;

                        var tMap = {};
                        //构建字典
                        for (var i = 0; i < d.length; i++) {
                            var item = d[i];
                            var tt = tMap[item.parentId] || {child: []};
                            tt.child[tt.child.length] = {pos: i, resId: item.resId, o: item.order};
                            tMap[item.parentId] = tt;
                        }

                        //构建3级tree（n)
                        function list2Tree(data, dict, key, level, idStr) {
                            var child = [];
                            level--;
                            var tmp = dict[key];
                            if (tmp && tmp['child']) {
                                tmp = tmp.child;
                                for (var j = 0; j < tmp.length; j++) {
                                    var c = data[tmp[j].pos]
                                    c['id'] = (idStr == "" ? "" : (idStr + "_")) + j;
                                    if (level > 0) {
                                        c['children'] = list2Tree(data, dict, c.resId, level, c['id']);
                                    }
                                    child[child.length] = c;
                                }
                            }
                            return child;
                        }

                        var root = list2Tree(d, tMap, 0, app.menuLevel, "");
                        callback(root);  // 参数是数组类型
                    });

                }
            });

            treeTable.on('tool(dataList)',
                function (obj) {
                    var data = obj.data
                        , event = obj.event //事件
                    ;
                    switch (event) {
                        case 'edit':
                            xadmin.open('修改角色', rootPath + 'system/res/edit?resId=' + data.resId)
                            break;
                        case 'detail':
                            xadmin.open('角色详情', rootPath + 'system/res/info?resId=' + data.resId)
                            break;
                        case 'del':
                            layer.msg('你确定删除么？', {
                                time: 2000,//2秒自动关闭
                                btn: ['确定', '取消'],
                                yes: function (index) {
                                    console.log(data);
                                    $.ajax({
                                        url: rootPath + 'system/res/del?resId=' + data.resId,
                                        type: "DELETE",
                                        dataType: "json",
                                        success: function (data) {
                                            console.log(data);
                                            layer.msg(data.message);
                                            location.reload(); //删除成功后再刷新
                                        },
                                        error: function (data) {
                                            layer.msg(data.message);
                                        }
                                    });
                                    layer.close(index);
                                }
                            })
                            break;

                    }
                });

            // 全部展开
            var expand = false;
            $('#btnExpandOrFold').click(function () {
                expand = !expand;
                if (expand) {
                    insTb.expandAll();
                } else {
                    insTb.foldAll();
                }
            });
            $('#btnSearch').click(function () {
                var keywords = $('#edtSearch').val();
                if (keywords) {
                    insTb.filterData(keywords);
                } else {
                    insTb.clearFilter();
                }
            });

            $('#btnClearSearch').click(function () {
                insTb.clearFilter();
            });
        });
    </script>
</th:block>
</html>